/**
 * Notes: 导出数据管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux@qq.com
 * Date: 2022-01-22 10:20:00 
 */

const BaseAdminService = require('./base_admin_service.js');
const UserModel = require('../../model/user_model.js');
const JoinModel = require('../../model/join_model.js');
const timeUtil = require('../../../framework/utils/time_util.js');
const util = require('../../../framework/utils/util.js');
const cloudUtil = require('../../../framework/cloud/cloud_util.js');

class AdminExportService extends BaseAdminService {

    /** 导出用户数据 */
    async exportUserDataExcel(condition) {
        let where = JSON.parse(decodeURIComponent(condition));

        // 取得数据
        let list = await UserModel.getAll(where, 'USER_ADD_TIME,USER_NAME,USER_MOBILE,USER_MEMO');

        // 表头
        let header = ['注册时间', '姓名', '手机', '备注'];

        // 表数据
        let data = [];
        for (let k in list) {
            let row = [];
            row.push(timeUtil.timestamp2Time(list[k].USER_ADD_TIME));
            row.push(list[k].USER_NAME);
            row.push(list[k].USER_MOBILE);
            row.push(list[k].USER_MEMO);
            data.push(row);
        }

        // 生成excel
        let fileName = '用户数据' + timeUtil.time('YMD') + '.xlsx';
        return await cloudUtil.exportExcel(fileName, header, data);
    }

    /** 导出预约数据 */
    async exportJoinDataExcel({
        meetId,
        startDay,
        endDay,
        status
    }) {
        let where = {
            JOIN_MEET_ID: meetId,
            JOIN_MEET_DAY: ['between', startDay, endDay]
        };
        if (status !== -1) where.JOIN_STATUS = status;

        // 取得数据
        let list = await JoinModel.getAll(where, 'JOIN_MEET_TITLE,JOIN_MEET_DAY,JOIN_MEET_TIME_START,JOIN_MEET_TIME_END,JOIN_USER_NAME,JOIN_STATUS,JOIN_ADD_TIME,JOIN_FORMS');

        // 表头
        let header = ['预约项目', '预约日期', '时段', '姓名', '状态', '提交时间', '预约表单'];

        // 表数据
        let data = [];
        for (let k in list) {
            let row = [];
            row.push(list[k].JOIN_MEET_TITLE);
            row.push(list[k].JOIN_MEET_DAY);
            row.push(list[k].JOIN_MEET_TIME_START + '-' + list[k].JOIN_MEET_TIME_END);
            row.push(list[k].JOIN_USER_NAME);
            row.push(JoinModel.getDesc('STATUS', list[k].JOIN_STATUS));
            row.push(timeUtil.timestamp2Time(list[k].JOIN_ADD_TIME));

            let forms = list[k].JOIN_FORMS;
            let formStr = '';
            for (let f in forms) {
                formStr += forms[f].title + '=' + forms[f].val + ' | ';
            }
            row.push(formStr);

            data.push(row);
        }

        // 生成excel
        let fileName = '预约数据' + timeUtil.time('YMD') + '.xlsx';
        return await cloudUtil.exportExcel(fileName, header, data);
    }
}

module.exports = AdminExportService;